package cn.wolfcode.wolf2w.redisService.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogincException;
import cn.wolfcode.wolf2w.redisService.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.impl.UserlnfoServiceImpl;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {
    @Autowired
    protected StringRedisTemplate stringRedisTemplate;
    @Override
    public void setVerifyCode(String phone, String code) {

        //String key="verify-code:"+phone;
        String key = RedisKeys.VERIFY_CODE.join(phone);
        System.err.println(phone);
        //参数1: key , 参数2:  value 参数3:时间长度  参数4:时间单位
        stringRedisTemplate.opsForValue().set(key, code, RedisKeys.VERIFY_CODE.getTime(),TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
        String key = RedisKeys.VERIFY_CODE.join(phone);
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public void setToken(String token, UserInfo userInfo) {
        String key = RedisKeys.USER_LOGIN.join(token);
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(userInfo),RedisKeys.USER_LOGIN.getTime(),TimeUnit.SECONDS);
    }

    @Override
    public UserInfo getHeader(String doken) {
        String token = stringRedisTemplate.opsForValue().get(doken);
        UserInfo userInfo=  JSON.parseObject(token,UserInfo.class);
    /*    UserInfo userInfo = JSONArray.parseObject(token, UserInfo.class);*/
        if (userInfo!=null){
            stringRedisTemplate.opsForValue().set(doken,JSON.toJSONString(userInfo),RedisKeys.USER_LOGIN.getTime(),TimeUnit.SECONDS);
            return userInfo;
        }else {
            throw new LogincException("请先登录");

        }
    }

    @Override
    public boolean isNull(RedisKeys userLogcoent) {
        String key = userLogcoent.join("s");
        Boolean aBoolean = stringRedisTemplate.hasKey(key);
        return aBoolean;
    }

    @Override
    public void addCode(RedisKeys userLogcoent) {

        String key = userLogcoent.join("s");
        stringRedisTemplate.opsForValue().set(key,"5");
    }

    @Override
    public void subtract() {
        String s = RedisKeys.USER_LOGCOENT.join("s");
        String s1 = stringRedisTemplate.opsForValue().get(s);
        int i = Integer.valueOf(s1);
        if (i==0){
            stringRedisTemplate.opsForValue().set(s,s1,1*60L,TimeUnit.SECONDS);
            throw new LogincException("次数已上线,请一分钟后再试");

        }else {
            i--;
            stringRedisTemplate.opsForValue().set(s,String.valueOf(i));
        }

    }

}
